﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.QLNH_BUS_WS;
using System.Threading;

namespace QLNH.MyLayouts
{
    public partial class LayoutReception : LayoutUC
    {
        public LayoutReception(MainForm mainForm)
        {
            InitializeComponent();
            this.mainForm = mainForm;
        }

        private void getAllTable()
        {
            Thread t = new Thread(new ThreadStart(getAllTableThread));
            t.SetApartmentState(ApartmentState.MTA);
            t.IsBackground = true;
            t.Start();
        }

        private void getAllTableThread()
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            TableDTO[] tables = ws.GetAllTable();
            TableDTO[] emptyTables = ws.GetAllEmptyTable();

            List<TableDTO> temp = tables.ToList<TableDTO>();

            for (int i = 0; i < emptyTables.Length; i++)
            {
                temp.Remove(temp.Find(t => t.ID == emptyTables[i].ID));

                ListViewItem li = new ListViewItem()
                {
                    Text = emptyTables[i].ID,
                    Name = emptyTables[i].ID,
                    Group = lv_tables.Groups["Empty"],
                    ImageKey = "Empty"
                };
                li.SubItems.Add(emptyTables[i].Capacity.ToString());
                li.SubItems.Add("Trống");

                lv_tables.BeginInvoke((MethodInvoker)(() => lv_tables.Items.Add(li)));
            }

            for (int i = 0; i < temp.Count; i++)
            {
                ListViewItem li = new ListViewItem()
                {
                    Text = temp[i].ID,
                    Name = temp[i].ID,
                    ImageKey = "Full",
                    Group = lv_tables.Groups["Full"]                   
                };
                li.SubItems.Add(temp[i].Capacity.ToString());
                li.SubItems.Add("Có khách");

                lv_tables.BeginInvoke((MethodInvoker)(() => lv_tables.Items.Add(li)));
            }
        }

        //public override void Resize(Size size)
        //{
        //    if (this.Size.Width < size.Width)
        //    {
        //        this.Location = new Point(size.Width / 2 - this.Size.Width / 2, size.Height / 2 - this.Size.Height / 2);
        //    }
        //    else
        //    {
        //        this.Location = new Point(0, 0);
        //    }
        //}

        public override void OnReceivePackage(IRemoting.BasePackage package)
        {
            if (package.getBroadcastMassageHeader() == IRemoting.BroadcastMassgeHeader.NEW_ORDER)
            {
                int orderNo = Int32.Parse(package.getBroadcastMassage());

                QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
                OrderDetailDTO order = ws.GetOrder(orderNo);

                ListViewItem li;
                lv_tables.BeginInvoke((MethodInvoker)(() =>
                    {
                        li = lv_tables.Items.Find(order.TableID, false)[0];
                        li.SubItems[2].Text = "Có khách";
                        li.Group = lv_tables.Groups["Full"];
                        li.ImageKey = "Full";
                    }));
            }
            else if (package.getBroadcastMassageHeader() == IRemoting.BroadcastMassgeHeader.CHECKED_OUT_ORDER)
            {
                string tableID = package.getBroadcastMassage();

                ListViewItem li;
                lv_tables.BeginInvoke((MethodInvoker)(() =>
                {
                    li = lv_tables.Items.Find(tableID, false)[0];
                    li.SubItems[2].Text = "Trống";
                    li.Group = lv_tables.Groups["Empty"];
                    li.ImageKey = "Empty";
                }));
            }
        }

        private void LayoutReception_Load(object sender, EventArgs e)
        {
            getAllTable();
        }
    }
}
